#include <vector>
#include <algorithm>
using namespace std;
/*
 * @lc app=leetcode.cn id=39 lang=cpp
 *
 * [39] 组合总和
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> res;
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<int> n;
        // sort(candidates.begin(), candidates.end());
        dfs(n, candidates, target, 0);
        return res;
    }

    void dfs(vector<int> & now, vector<int>& candidates, int target, int index) {
        if (target == 0 && now.size() > 0) {
            res.push_back(now);
            return;
        }
        if (index >= candidates.size()) return;
        if (target >= candidates[index]) {
            now.push_back(candidates[index]);
            dfs(now, candidates, target - candidates[index], index);
            now.pop_back();
        }
        dfs(now, candidates, target, index + 1);
    }
};
// @lc code=end

